package com.happyass.controller;

import com.happyass.common.entity.ResultEntity;
import com.happyass.dao.EntertainmentDao;
import com.happyass.dao.FacilityDao;
import com.happyass.pojo.Entertainment;
import com.happyass.pojo.Facility;
import com.happyass.pojo.Staff;
import com.happyass.service.ExcelService;
import com.happyass.service.StaffInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    EntertainmentDao entertainmentDao;
    @Autowired
    FacilityDao facilityDao;
    @Autowired
    ExcelService excelService;
    @Autowired
    StaffInfoService staffInfoService;

    //访问员工登录页面
    @RequestMapping("/login")
    public String staffLogin(){
        return "/Login/login";
    }
    //访问员工登录主页面
    @RequestMapping("/loginSubmit")
    @ResponseBody
    public HashMap<String, Object> staffLoginMain(Staff staff, HttpServletRequest request){

        return staffInfoService.login(staff,request);
    }
    //访问员工注册页面
    @RequestMapping("/register")
    public String staffRegister(){

        return "/Login/register";
    }
    //访问员工登录主页面
    @RequestMapping("/main")
    public String staffLoginMain(){

        return "/Login/main";
    }

    //访问欢迎页面
    @RequestMapping("/welcome")
    public String welcome(){

        return "/Login/welcome";
    }

    //退出
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, ModelMap modelMap){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/manager/login";
    }
    @RequestMapping("/entertainment")
    public String entertainment(Model model){
        List<Entertainment> all = entertainmentDao.findAll();
        model.addAttribute("entertainment", all);
        return "/entertain/entertainment";
    }
    @RequestMapping("/facility")
    public String facility(Model model){
        List<Facility> all = facilityDao.findAll();
        model.addAttribute("facility", all);
        return "/entertain/facility";
    }
    @RequestMapping(value = "/facilitystatus", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Facility> facilitystatus(@RequestBody Facility facility){
        Facility facility1=facilityDao.getOne(facility.getFacilityId());
        facility1.setFacilityStatus(facility.getFacilityStatus());
        facilityDao.save(facility1);
        return new ResultEntity<Facility>(ResultEntity.ResultStatus.SUCCESS.status,
                "修改成功");
    }
    @RequestMapping("/facilitymodify")
    public String facilitymodify(Model model,Integer id){
        Facility facility = facilityDao.getOne(id);
        List<Entertainment> all = entertainmentDao.findAll();
        model.addAttribute("entertainment",all);
        model.addAttribute("onefacility", facility);
        return "/entertain/facilitymodify";
    }
    @RequestMapping("/entertainmentmodify")
    public String entertainmentmodify(Model model,Integer id){
        Entertainment entertainment = entertainmentDao.getOne(id);
        model.addAttribute("oneentertainment",entertainment);
        return "/entertain/entertainmentmodify";
    }
    @RequestMapping("/addfacility")
    public String addfacility(Model model) {
        List<Entertainment> all = entertainmentDao.findAll();
        model.addAttribute("entertainment",all);
        return "/entertain/addfacility";
    }
    @RequestMapping("/addentertainment")
    public String addentertainment(Model model) {
        return "/entertain/addentertainment";
    }
    @RequestMapping(value = "/delfacility", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Facility> delfacility(@RequestBody Integer id){
        facilityDao.deleteById(id);
        return new ResultEntity<Facility>(ResultEntity.ResultStatus.SUCCESS.status,
                "删除成功");
    }
    @RequestMapping(value = "/delentertainment", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Entertainment> delentertainment(@RequestBody Integer id){
        entertainmentDao.deleteById(id);
        return new ResultEntity<Entertainment>(ResultEntity.ResultStatus.SUCCESS.status,
                "删除成功");
    }
    @RequestMapping(value = "/addf", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Facility> addf(@RequestBody Facility facility){
        List<Entertainment> entertainmentList = facility.getEntertainmentList();
        Integer entertainmentId = facility.getEntertainmentList().get(0).getEntertainmentId();
        Entertainment entertainmentDaoOne = entertainmentDao.getOne(entertainmentId);
        facility.setEntertainmentList(null);
        entertainmentList.clear();
        entertainmentList.add(entertainmentDaoOne);
        facility.setEntertainmentList(entertainmentList);
        facilityDao.save(facility);
        return new ResultEntity<Facility>(ResultEntity.ResultStatus.SUCCESS.status,
                "添加成功");
    }
    @RequestMapping(value = "/adde", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Entertainment> adde(@RequestBody Entertainment entertainment){
        entertainmentDao.save(entertainment);
        return new ResultEntity<Entertainment>(ResultEntity.ResultStatus.SUCCESS.status,
                "添加成功");
    }
    @RequestMapping(value = "/modifyf", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Facility> modifyf(@RequestBody Facility facility){
        List<Entertainment> entertainmentList = facility.getEntertainmentList();
        Integer entertainmentId = facility.getEntertainmentList().get(0).getEntertainmentId();
        Entertainment entertainmentDaoOne = entertainmentDao.getOne(entertainmentId);
        facility.setEntertainmentList(null);
        entertainmentList.clear();
        entertainmentList.add(entertainmentDaoOne);
        facility.setEntertainmentList(entertainmentList);
        facilityDao.save(facility);
        return new ResultEntity<Facility>(ResultEntity.ResultStatus.SUCCESS.status,
                "修改成功");
    }
    @RequestMapping(value = "/modifye", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResultEntity<Entertainment> modifye(@RequestBody Entertainment entertainment){
        List<Facility> facilityList = entertainment.getFacilityList();
        entertainmentDao.save(entertainment);
        return new ResultEntity<Entertainment>(ResultEntity.ResultStatus.SUCCESS.status,
                "修改成功");
    }
    //excel导出
    @RequestMapping(value ="/excel")
    @ResponseBody
    public void excel(HttpServletResponse response,@RequestParam Integer a) throws IOException {
        excelService.excel(response,a);
    }

}
